package com.liangxy.zhyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liangxy.zhyl.base.PageResponse;
import com.liangxy.zhyl.dto.NursingProjectPlanDto;
import com.liangxy.zhyl.entity.NursingPlan;
import com.liangxy.zhyl.entity.NursingProjectPlan;
import com.liangxy.zhyl.mapper.NursingPlanMapper;
import com.liangxy.zhyl.mapper.NursingProjectPlanMapper;
import com.liangxy.zhyl.service.NursingPlanService;
import com.liangxy.zhyl.utils.UserThreadLocal;
import com.liangxy.zhyl.vo.NursingPlanVo;
import com.liangxy.zhyl.vo.NursingProjectPlanVo;
import com.liangxy.zhyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询所有的护理计划
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAll() {
        return nursingPlanMapper.selectAll();
    }

    /**
     * 分页查询项目计划
     * @param planName
     * @param status
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> pageNursingPlan(String planName, Integer status, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.pageNursingPlan(planName, status);
        return PageResponse.of(page,NursingPlanVo.class);
    }

    /**
     * 根据id查询护理计划及其包含的项目
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo selectNursingPlan(Long id) {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        // 根据id查找护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlan(id);
        // 赋值给plan基本信息
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        // 根据id查找计划所包含的项目
        List<NursingProjectPlanVo> nursingProjectVos = nursingProjectPlanMapper.selectByPlanId(id);

        nursingPlanVo.setProjectPlans(nursingProjectVos);
        return nursingPlanVo;
    }
    /**
     * 添加护理计划
     * @param nursingProjectPlanDto
     */
    @Override
    @Transactional
    public void addNursingPlan(NursingProjectPlanDto nursingProjectPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingProjectPlanDto,nursingPlan);
        // 先插入护理计划
        nursingPlanMapper.addNursingPlan(nursingPlan);
        // 依次往集合里的数据添加planId
        List<NursingProjectPlan> list = nursingProjectPlanDto.getProjectPlans().stream().map(v -> {
            NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
            BeanUtils.copyProperties(v,nursingProjectPlan);
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlan.setCreateBy(UserThreadLocal.getUserId());
            return nursingProjectPlan;
        }).collect(Collectors.toList());
        // 批量插入计划所包含的项目
        nursingProjectPlanMapper.insertBatch(list);
    }

    /**
     * 禁用启用护理计划
     * @param id
     * @param status
     */
    @Override
    public void updateNursingPlanStatus(Long id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setUpdateBy(UserThreadLocal.getMgtUserId());
        nursingPlanMapper.update(nursingPlan);
    }

    /**
     * 修改护理计划
     * @param id
     * @param nursingProjectPlanDto
     */
    @Override
    @Transactional
    public void updateNursingPlan(Long id, NursingProjectPlanDto nursingProjectPlanDto) {
        // 更新护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingProjectPlanDto, nursingPlan);
        nursingPlan.setId(id);
        nursingPlanMapper.update(nursingPlan);
        // 更新护理计划  里 的 项目
        // 1.删除旧的项目 planId
        nursingProjectPlanMapper.deleteByPlanId(id);
        // 2.批量添加新的项目
        List<NursingProjectPlan> list = nursingProjectPlanDto.getProjectPlans().stream().map(p -> {
            NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
            BeanUtils.copyProperties(p, nursingProjectPlan);
            nursingProjectPlan.setPlanId(id);
            return nursingProjectPlan;
        }).collect(Collectors.toList());
        nursingProjectPlanMapper.insertBatch(list);
    }

    /**
     * 删除护理计划
     *
     * @param id
     */
    @Override
    @Transactional
    public void deleteNursingPlan(Long id) {
        // 删除计划
        nursingPlanMapper.deleteById(id);
        // 删除计划下的项目计划
        nursingProjectPlanMapper.deleteByPlanId(id);
    }
}
